﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    [Table("T_Reckoning")]
    public class T_Reckoning : IEntity<T_Reckoning>
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        /// <summary>
        /// 账单主人
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string Own { get; set; }

        /// <summary>
        /// 支付类型
        /// 0：为稿件支付
        /// 1：期刊年费
        /// </summary>
        public int PayFor { get; set; }

        /// <summary>
        /// 关联ID
        /// payfor = 0  ： Manuscript.Id
        /// </summary>
        public int RaletionId { get; set; }

        [Column(TypeName="datetime")]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 支付时间
        /// </summary>
        [Column(TypeName="datetime")]
        public DateTime PayTime { get; set; }

        /// <summary>
        /// 支付额度
        /// </summary>
        public int TotalPay { get; set; }

        /// <summary>
        /// 支付类型
        /// 1：支付宝线上支付
        /// 9：线下支付
        /// </summary>
        public int PayType { get; set; }

        /// <summary>
        /// 账单状态
        /// 0：待支付
        /// 1：已支付待确认
        /// 2：支付完成
        /// 9：已关闭
        /// </summary>
        public int Status { get; set; }

        /// <summary>
        /// 支付交易号
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string Txid { get; set; }

        public int PaymentStreamId { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string Message { get; set; }

        public override T_Reckoning Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Reckoning Find(System.Linq.Expressions.Expression<Func<T_Reckoning, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Reckoning.FirstOrDefault(where);
                if (item != null)
                {
                    this.CreateTime = item.CreateTime;
                    this.Id = item.Id;
                    this.Own = item.Own;
                    this.PayFor = item.PayFor;
                    this.PaymentStreamId = item.PaymentStreamId;
                    this.PayTime = item.PayTime;
                    this.PayType = item.PayType;
                    this.RaletionId = item.RaletionId;
                    this.Status = item.Status;
                    this.TotalPay = item.TotalPay;
                    this.Txid = item.Txid;
                    this.Message = item.Message;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Reckoning, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Reckoning.Any(where);
            }
        }
    }
}
